// pages/equipItem/equipItem.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:true,
    selGoodsList:[],
    myPartList:[],
    equipBtns: ['changeLocation', 'offLine', 'changeParts', 'maintain', 'changeRfid', 'noReturn', 'confirmLost', 'rentList', 'visitList', 'monthRent'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options",options)
    let that = this
    that.setData({
      equipCode: options.equipCode.replace(/L/ig, "")
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    that.toast = that.selectComponent("#toast")
    that.personalModal = that.selectComponent("#personalModal")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.getEquipInfo();
    //添加物品
    if (that.data.addGoodsFlag) {
      let list = that.data.selGoodsList
      that.setData({
        show: false,
      })
      util.print('selGoodsList',list);
      for (var i = 0; i < list.length; i++) {
        that.selGoodsAdd(list[i]);
      }
      that.data.selGoodsList = [];
      that.setData({
        show: true,
      })
    }
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      partList:[]
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  
  selGoodsAdd: function (selectGoods) {
    let index = this.data.myPartList.findIndex(item=>item.id==selectGoods.id);
    if(index>-1){
      this.data.myPartList[index].accNum = selectGoods.accNum;
    } else {
      this.data.myPartList.push(selectGoods);
    }
    this.setData({
      myPartList: this.data.myPartList
    });
    this.data.addGoodsFlag = false;
  },
  /**
   * 清除已选择的配件
   */
  deleteMyPartList: function(){
    this.setData({
      myPartList:[]
    })
  },
  deleteParts: function (e) {
    let that = this;
    let id = e.detail;
    console.log('deleteParts', id);
    let myPartList = that.data.myPartList;
    myPartList.forEach((item, index) => {
      if (item.id == id) {
        myPartList.splice(index, 1);
      }
    })
    that.setData({
      myPartList: myPartList
    })
  },
  /**
   * 获取设备详情
   */
  getEquipInfo: function () {
    let that = this
    lbbService.getEquipDetailInfo({ equId: that.data.equipCode }, function (res) {
      that.setData({
        equipInfo: res.data
      })
    })
  },

})